import {createSql} from "./index";
import type {Group} from "../types";

const tableName = "t_group";

export const queryList = () =>
    createSql<Group>('SELECT * FROM ' + tableName + '  ORDER BY `rank` ASC');

export async function getRank(){
    const GroupList = await createSql<Group[]>('SELECT * FROM ' + tableName + ' ORDER BY `rank` DESC LIMIT 1')
    return GroupList.length > 0 ? GroupList[0].rank + 1 : 1;
}

export const insert = async (data: Group) =>{
    return  createSql<Group>(
        'INSERT INTO '+ tableName + ' (id, name, status ,createTime,`rank`) VALUES ( ?,?,?,?,?)',
        [data.id, data.name, data.status,data.createTime,data.rank]
    );
}
export const deleteById = (id: string) =>{
    return createSql<Group>(' DELETE FROM '+ tableName + ' WHERE id = ?', [id]);
}

export const update = async (data: Group) =>{
    return  createSql<Group>(
        'UPDATE '+ tableName + ' SET status = ? WHERE id = ? ',
        [ data.status, data.id]
    );
}

export default {
    queryList,
    insert,
    deleteById,
    getRank,
    update,
}
